package com.example.demo.Controller;

import com.example.demo.Bean.Seckill;
import com.example.demo.Bean.Shop;
import com.example.demo.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@RestController
public class ShopController {
    @Autowired
    private UserService userService;

    @PostMapping("/shop")
    public Shop shop(String name){
        return userService.getShopByName(name);
    }

    @PostMapping("/insertSeckill")
    public Boolean insertSeckill(String shopname,Integer product_id,String category,Double price,Integer product_nums,String start) throws ParseException {
        try{
            Calendar calendar = new GregorianCalendar();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date parse = sdf.parse(start);
            calendar.setTime(parse);
            calendar.add(calendar.DATE,1);
            Date time = calendar.getTime();
            String end = sdf.format(time);
            Seckill seckill = new Seckill();
            seckill.setShopname(shopname);
            seckill.setCategory(category);
            seckill.setProduct_id(product_id);
            seckill.setPrice(price);
            seckill.setProduct_nums(product_nums);
            seckill.setStart(start);
            seckill.setEnd(end);
            userService.insertSeckill(seckill);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
